<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Userreg
 *
 * @author thanhlnh
 */
class Model_Userreg {
    
    static $TABLE_NAME = 'user_reg';
    public function insert_new_user($data){
        if (!empty($data)) {
            $db = Model_Connections::getInstance()->getDbConnect();
            $numRows = $db->insert(self::$TABLE_NAME, $data);
            $lastInsertId = -1;
            if ($numRows > 0)
                $lastInsertId = $db->lastInsertId();
            return $lastInsertId;
        }
    }
    
    public function get_user_by_email($email){
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * From ".self::$TABLE_NAME." WHERE user_email = '".$email."'";
        return $db->fetchAll($sql);
    }   
    
    public function get_user_total() {
        $db = Model_Connections::getInstance()->getDbConnect();
        $count = $db->fetchOne('SELECT COUNT(*) AS count FROM ' . self::$TABLE_NAME);
        return $count;
    }
    
    public function get_user_by_page($page, $count) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * from " . self::$TABLE_NAME;
        $start_from = ($page - 1) * $count;
        $end = $count;
        $sql .= ' order by id DESC ';
        $sql .= ' LIMIT ' . $start_from . ',' . $end;
        $data = $db->fetchAll($sql);
        return $data;
    }
    
    public function get_user_by_id($id){
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * From ".self::$TABLE_NAME." WHERE id = '".$id."'";
        return $db->fetchAll($sql);
    }
    
    public function update_user($info){
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Update " . self::$TABLE_NAME;
        $sql .= " SET ";
        $sql .= "`user_phone`" . "='" . $info['user_phone'] . "',";
        $sql .= "`user_phoneinfo`" . "='" . $info['user_phoneinfo'] . "',";
        $sql .= "`user_deviceid`" . "='" . $info['user_deviceid'] . "'";
        $sql .= ' where id = ' . $info['id'];

        return $db->query($sql);
    }
}

?>
